﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace OA.Mvc.Controllers
{
    public class AccountController : Controller
    {
        [AllowAnonymous]  // 允许匿名
        [HttpGet]
        public ActionResult Login()
        {
            throw new Exception("一个错误");
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public ActionResult Login(string name,string pwd,string returnUrl=null)
        {
            if (name=="admin")
            {
                // FormsAuthentication.SetAuthCookie(name, false);// 基本信息登录，只记录用户名

                var ticket = new FormsAuthenticationTicket(1, name, DateTime.Now, DateTime.Now.AddMinutes(20), false, "2,1");

                var entry = FormsAuthentication.Encrypt(ticket);  // 加密
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, entry);
                Response.Cookies.Add(cookie);  // 写入客户端cookie

                if (string.IsNullOrEmpty(returnUrl))
                    return RedirectToAction("Index", "Home");
                return Redirect(returnUrl);
            }
            return View();
        }
    }
}